<?php
/**
 * Created by PhpStorm.
 * @User: 王崇全
 * @Date: 2018/12/18 11:36
 */

namespace app\adm\controller;


use app\adm\common\controller\BaseController;
use app\common\model\Article;
use app\common\model\ArticleCategory;

class ArticleController extends BaseController
{

    /**
     * 添加栏目
     * @author 王崇全
     * @date   2018/12/18 11:43
     * @return void
     * @throws \Exception
     */
    public function addCatalog()
    {

        $in = $this->I([
            [
                "title|栏目标题",
                null,
                "s",
                "require",
            ],
            [
                "pid|上级栏目ID",
                null,
                "d",
            ],
        ]);

        $ac = new ArticleCategory();
        $ac->addCategory($in["title"], $in["pid"]);

        rj();
    }

    /**
     * 删除栏目
     * @author 王崇全
     * @date   2018/12/18 11:49
     * @return void
     * @throws \Exception
     */
    public function delCatalog()
    {
        $in = $this->I([
            [
                "cid|栏目ID",
                null,
                "d",
                "require",
            ],
        ]);

        //s1 检查栏目下是否有栏目或者文章
        //todo:并未考虑多级栏目的情况
        $a    = new Article();
        $list = $a->getList([
            "cid" => $in["cid"],
        ], 1, 1);
        if ($list[1] != 0) {
            throw new \Exception("此栏目下有文章, 请先清空文章");
        }

        //s2 检查栏目是否关联首页
        /*$c    = new Config();
        $flag = $c->checkValExist([
            "cid_gsdt",
            "cid_rddt",
            "cid_hyxw",
        ], $in["cid"]);
        if ($flag) {
            throw new \Exception("此栏目已与首页关联, 请解除后再删除");
        }*/

        //s3 删除栏目
        $ac = new ArticleCategory();
        $ac->delCategory($in["cid"]);

        rj();
    }

    /**
     * 编辑栏目
     * @author 王崇全
     * @date   2018/12/18 11:55
     * @return void
     * @throws \Exception
     */
    public function editCatalog()
    {
        $in = $this->I([
            [
                "cid|栏目ID",
                null,
                "d",
                "require",
            ],
            [
                "pid|上级栏目ID",
                null,
                "d",
            ],
            [
                "title|栏目标题",
                null,
                "s",
            ],
        ]);

        $ac = new ArticleCategory();
        $ac->editCategory($in["cid"], $in["title"], $in["pid"]);

        rj();
    }

    /**
     * 栏目列表
     * @author 王崇全
     * @date   2018/12/18 11:59
     * @return void
     */
    public function listCatalog()
    {
        $in = $this->I([
            [
                "pid|上级栏目ID",
                null,
                "d",
            ],
        ]);

        $ac   = new ArticleCategory();
        $list = $ac->categoryList($in["pid"]);

        rjd($list);
    }

    /**
     * 栏目信息
     * @author 王崇全
     * @date   2018/12/18 11:59
     * @return void
     */
    public function catalogInfo()
    {
        $in = $this->I([
            [
                "cid|栏目ID",
                null,
                "d",
                "require",
            ],
        ]);

        $ac   = new ArticleCategory();
        $info = $ac->catalogInfo($in["cid"]);

        rjd($info);
    }


    /**
     * 发布文章
     * @author 王崇全
     * @date   2018/12/19 16:43
     * @return void
     */
    public function addArticle()
    {

        $in = $this->I([
            [
                "cid|栏目ID",
                null,
                "d",
                "require",
            ],
            [
                "title|文章标题",
                null,
                "s",
                "require",
            ],
            [
                "cover|文章封面",
                null,
                "s",
            ],
            [
                "content|文章内容",
                null,
                "s",
                "require",
            ],
        ]);

        $a    = new Article();
        $info = $a->add($in["title"], $in["cid"], $in["content"], $in["cover"]);

        rjd($info);
    }

    /**
     * 删除文章
     * @author 王崇全
     * @date   2018/12/20 10:45
     * @return void
     */
    public function delArticle()
    {
        $in = $this->I([
            [
                "id|文章ID",
                null,
                "s",
                "require",
            ],
        ]);

        $a = new Article();
        $a->del($in["id"]);

        rj();
    }

    /**
     * 修改文章
     * @author 王崇全
     * @date   2018/12/20 9:54
     * @return void
     * @throws \Exception
     */
    public function editArticle()
    {
        $in = $this->I([
            [
                "id|文章ID",
                null,
                "s",
                "require",
            ],
            [
                "cid|栏目ID",
                null,
                "d",
            ],
            [
                "title|文章标题",
                null,
                "s",
            ],
            [
                "cover|文章封面",
                null,
                "s",
            ],
            [
                "content|文章内容",
                null,
                "s",
            ],
        ]);

        $a    = new Article();
        $info = $a->edit($in["id"], $in["title"], $in["cid"], $in["content"], $in["cover"]);

        rjd($info);

    }

    /**
     * 文章信息
     * @author 王崇全
     * @date   2018/12/20 10:44
     * @return void
     * @throws \Exception
     */
    public function info()
    {
        $in = $this->I([
            [
                "id|文章ID",
                null,
                "s",
                "require",
            ],
        ]);

        $a    = new Article();
        $info = $a->getInfo($in["id"]);

        rjd($info);
    }

    /**
     * 文章列表
     * @author 王崇全
     * @date   2018/12/20 10:44
     * @return void
     */
    public function listArticle()
    {

        $in = $this->I([
            [
                "cid|栏目ID",
                null,
                "d",
            ],
            [
                "title|标题关键词",
                null,
                "s",
            ],
            [
                "st|起始时间",
                null,
                "s",
            ],
            [
                "et|截止时间",
                null,
                "s",
            ],
            [
                "pageNo|页码",
                DEF_PAGE_NO,
                "d",
            ],
            [
                "pageSize|页幅",
                DEF_PAGE_SIZE,
                "d",
            ],
        ]);

        $a    = new Article();
        $list = $a->getList([
            "cid"   => $in["cid"],
            "st"    => $in["st"],
            "et"    => $in["et"],
            "title" => trim($in["title"]),
        ], $in["pageNo"], $in["pageSize"]);

        //梳理内容
        foreach ($list[0] as &$v) {
            $v["content"] = htmlspecialchars_decode($v["content"]);//把一些预定义的 HTML 实体转换为字符
            $v["content"] = str_replace("&nbsp;", "", $v["content"]);//将空格替换成空
            $v["content"] = strip_tags($v["content"]);//函数剥去字符串中的 HTML、XML 以及 PHP 的标签,获取纯文本内容
            $v["content"] = mb_substr($v["content"], 0, 40, "utf-8");//返回字符串中的前100字符串长度的字符
            if (mb_strlen($v["content"]) == 40) {
                $v["content"] .= "...";
            }
        }

        rjd([
            "list"  => $list[0],
            "count" => $list[1],
        ]);
    }

}